package org.test.beans;

import java.io.Serializable;
import java.util.Random;

import javax.annotation.PostConstruct;
import javax.faces.bean.CustomScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.event.ActionEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import com.icesoft.faces.component.tree.IceUserObject;

@ManagedBean(name = TreeBean.BEAN_NAME)
@CustomScoped(value = "#{window}")
public class TreeBean implements Serializable {

    public static final String BEAN_NAME = "tree";
    public static final String IMAGE_PATH = "/xmlhttp/css/rime/css-images/";
    public static final String IMAGE_ICON = "tree_document.gif";
    public static final String IMAGE_CLOSED = "tree_folder_closed.gif";
    public static final String IMAGE_OPEN = "tree_folder_open.gif";

    private static final Random randomizer = new Random(System.nanoTime());
    private DefaultTreeModel model;

    public TreeBean() {
        model = generateRandomTree();
    }


    public static DefaultTreeModel generateRandomTree() {
        return generateRandomTree(2, 3, 1, 5);
    }

    public void refreshTree(ActionEvent event) {
        model = generateRandomTree();
    }

    public static DefaultTreeModel generateRandomTree(int minFolder, int randFolder,
                                                      int minLeaf, int randLeaf) {
        // Setup the root node
        DefaultMutableTreeNode rootNode = addNode(null, "Root", false);
        DefaultTreeModel toReturn = new DefaultTreeModel(rootNode);

        // Setup each folder and child
        DefaultMutableTreeNode folderNode = null;
        int numFolders = minFolder + randomizer.nextInt(randFolder);
        int numLeaf = -1;
        for (int i = 0; i < numFolders; i++) {
            folderNode = addNode(rootNode, "Folder " + (i + 1), false);

            numLeaf = minLeaf + randomizer.nextInt(randLeaf);

            for (int j = 0; j < numLeaf; j++) {
                addNode(folderNode, "Item " + (i + 1) + "-" + j, true);
            }
        }
        return toReturn;
    }

    public static DefaultMutableTreeNode addNodeToRoot(DefaultTreeModel model, String text, boolean isLeaf) {
        return addNode((DefaultMutableTreeNode) model.getRoot(), text, isLeaf);
    }

    public static DefaultMutableTreeNode addNode(DefaultMutableTreeNode parent, String text, boolean isLeaf) {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode();
        IceUserObject userObject = new IceUserObject(node);
        node.setUserObject(userObject);

        // Customize our new node
        userObject.setLeaf(isLeaf);
        userObject.setText(text);
        userObject.setTooltip(text);
        node.setAllowsChildren(!isLeaf);

        // Set the image icons
        userObject.setLeafIcon(makeIcon(IMAGE_ICON));
        userObject.setBranchContractedIcon(makeIcon(IMAGE_CLOSED));
        userObject.setBranchExpandedIcon(makeIcon(IMAGE_OPEN));

        // Expand if we're not a leaf
        if (!isLeaf) {
            userObject.setExpanded(true);
        }

        // Add the completed node to the parent
        if (parent != null) {
            parent.add(node);
        }

        return node;
    }

    public static String makeIcon(String image) {
        return IMAGE_PATH + image;
    }

    public DefaultTreeModel getModel() {
        return model;
    }

    public void setModel(DefaultTreeModel model) {
        this.model = model;
    }
}